/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.entites.Employee;
import com.entites.EventComment;
import com.entites.EventLibrary;
import com.model.bean.EventCommentFacadeLocal;
import com.model.bean.EventLibraryFacadeLocal;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author NguyenVan
 */
@ManagedBean
@SessionScoped
public class EventLibraryDetailsController {

    @EJB
    private EventLibraryFacadeLocal eventLibraryFacade;

    @EJB
    private EventCommentFacadeLocal eventCommentFacade;

    public EventLibraryDetailsController() {

    }

    EventLibrary ev;

    private int eventId;
    private String username;
    private String comment;
    private Date timeComment;
    private int pages;
    private int currentPages;
    private int noOfRecords;
    private int noOfPage;

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public int getCurrentPages() {
        return currentPages;
    }

    public void setCurrentPages(int currentPages) {
        this.currentPages = currentPages;
    }

    public int getNoOfRecords() {
        return noOfRecords;
    }

    public void setNoOfRecords(int noOfRecords) {
        this.noOfRecords = noOfRecords;
    }

    public int getNoOfPage() {
        return noOfPage;
    }

    public void setNoOfPage(int noOfPage) {
        this.noOfPage = noOfPage;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Date getTimeComment() {
        return timeComment;
    }

    public void setTimeComment(Date timeComment) {
        this.timeComment = timeComment;
    }

    public String getUsernameSession() {
        username = (String) session.getAttribute("username");
        return username;
    }

    public int getEventIdSession() {
        int id = (int) session.getAttribute("eventId");
        return id;
    }

    public EventLibrary getEventLibraryDetails() {
        ev = eventLibraryFacade.findById(getEventIdSession(),getUsernameSession());
        return ev;
    }

    public List<EventLibrary> getEventLibraryRelated(){
        String eventType=getEventLibraryDetails().getEventType();
        List<EventLibrary> list=eventLibraryFacade.findAllByEventType(eventType);
        List<EventLibrary>listev=new ArrayList<>();
        if(list.size()<=3){
            return listev=list;
        }else{
            int i=0;
            while(i==3){
                for(EventLibrary ev:list){
                    EventLibrary evl=new EventLibrary();
                    evl.setEventId(ev.getEventId());
                    evl.setName(ev.getName());
                    evl.setDescription(ev.getDescription());
                    evl.setEventType(ev.getEventType());
                    evl.setTimeStart(ev.getTimeStart());
                    evl.setTimeEnd(ev.getTimeEnd());
                    listev.add(evl);
                    i++;
                }
            }
            return listev;
        }
       
    }
    public boolean doComment() {
        try {
            EventComment evc = new EventComment(getEventIdSession(), getUsernameSession(), comment);
     
            if (getUsernameSession() != null && getEventIdSession() !=0) {
                eventCommentFacade.postComment(evc);
            } else {
                ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
                context.redirect(context.getRequestContextPath() + "/Login.xhtml");
            }
            comment="";

        } catch (IOException e) {
            System.out.println(""+e.getMessage());
        }

        return false;
    }

    public List<EventComment> getEventCommentList() {
        try {

            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("pages") != null) {
                pages = Integer.parseInt(request.getParameter("pages"));
            }
            List<EventComment> list;
            int record = 5;
            currentPages = 1;
            if (pages != 0) {
                currentPages = pages;
            }
            int start_row = (currentPages * record) - (record - 1);
            int end_row = start_row + record - 1;
            list = eventCommentFacade.getEventCommentListPaging(start_row, end_row, getEventIdSession());
            noOfRecords = eventCommentFacade.countCommentByEventId(getEventIdSession());
            noOfPage = ((int) Math.ceil(noOfRecords * 1.0 / record));

            return list;

        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

}
